<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="{{url_for('static',filename='js/jquery.min.js')}}"></script>
    <script src="{{url_for('static',filename='js/jquery.dataTables.min.js')}}"></script>

    <link rel="stylesheet" href="{{url_for('static',filename='css/jquery.dataTables.min.css')}}">
    <link rel="stylesheet" href="{{url_for('static',filename='css/bootstrap.min.css')}}">
    <link rel="stylesheet" href="{{url_for('static',filename='css/bootstrap-select.min.css')}}">
    <title>条件过滤</title>

</head>
<body>
        <nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
    <div class="navbar-header">
        <a class="navbar-brand" href="#">盯盘宝后台管理系统</a>
    </div>
    <div>
        <ul class="nav navbar-nav">
            <li ><a href="{{ url_for('start_web') }}">首页</a></li>
            <li ><a href="{{ url_for('permission_manage') }}">权限配置</a></li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                    管理员{{ session['admin'] if session['admin'] else "未登录" }}
                    <b class="caret"></b>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="{{ url_for("login_out") }}">退出</a></li>
                </ul>
            </li>
        </ul>
    </div>
    </div>
</nav>
<!--账户收益明细-->
<div id="test">
    <h3>条件过滤</h3>
    <div style="margin:auto;" align="center">
    <select id = "date" class="form-control" style="margin:0px 20px 0px 0px;width:auto;">
    </select>
    </div>
    <br/>
    <br/>
    <div id ="zhaban_rate" align="center">
    </div>
<div id="condition_select" style="border:1px solid rgb(68, 113, 188);">

    <div id="select_all" align="center">

    是否未开板的新股:<select id="lxzt">
        <option value="全部">全部</option>
        <option value="是">是</option>
        <option value="否">否</option>
        </select>

    所属概念排名:<select  id="order"  multiple class="selectpicker">
                </select>

    是否涨停价开盘:<select id="is_yzb">
        <option value="全部">全部</option>
        <option value="是">是</option>
        <option value="否">否</option>
        </select>
    封板资金:<select id="lzt_capital">
                    <option value="全部">全部</option>
                    <option value="小于阈值">小于阈值</option>
                    <option value="大于阈值">大于阈值</option>
                    </select>
    是否高位板:<select id="gaowei">
        <option value="全部">全部</option>
        <option value="是">是</option>
        <option value="否">否</option>
        </select>
    放量阴线10个交易日内:<select id="big_vol_shade_line">
        <option value="全部">全部</option>
        <option value="无">无</option>
        <option value="现象1">现象1</option>
        <option value="现象2">现象2</option>
        </select>
    是否有压力位:<select id="resistance_section">
        <option value="全部">全部</option>
        <option value="无">无</option>
        <option value="有">有</option>
        </select>
    严格是否买入:<select id="is_buy">
        <option value="全部">全部</option>
        <option value="已买入">已买入</option>
        <option value="未买入">未买入</option>
        </select>
    </div>
    <br/>
    <div align="center">
        <div class="btn-group btn-group-lg" role="group" aria-label="...">
        <div class="btn-group" role="group" align="center">
        <button type="button" class="btn btn-primary" onclick="refreshConditionFilterData('condition')">查询</button>
        </div>
        <div class="btn-group" role="group" align="center">
        <button type="button" class="btn btn-primary" onclick="clearCondition()">清空条件</button>
        </div>
        </div>

        <br/>
        <br/>
        </div>

</div>
        <div class="row" align="center" style="border:1px solid rgb(68, 113, 188);">
    <div id ="stock_checkbox"  style="width:80%;">

    </div>
        <br/>
    <div align="center">
        <div class="btn-group btn-group-lg" role="group" aria-label="...">
        <div class="btn-group" role="group" align="center">
        <button type="button" class="btn btn-primary" onclick="refreshConditionFilterData('stock')">查询</button>
        </div>
        <div class="btn-group" role="group" align="center">
        <button type="button" class="btn btn-primary" onclick="allIn(true)">全选</button>
        </div>
        <div class="btn-group" role="group" align="center">
        <button type="button" class="btn btn-primary" onclick="allIn(false)">全不选</button>
        </div>
        </div>
    </div>
        <br/>
    </div>
    <div align="center">
        <p style="font-size:22px;">程序执行顺序<span class="glyphicon glyphicon-arrow-right"></span></p>
    </div>
    <table id="example" border="1" class="display" style="width:100%">
        <thead>
        <tr>

            <th rowspan="2">宽松买入</th>
            <th rowspan="2">涨停时间</th>
            <th rowspan="2">选股报前瞻概念</th>
            <th rowspan="2">是否未开板新股</th>
            <th rowspan="2">所属概念排名(阈值2,其中一个概念排名前2即可)</th>
            <th rowspan="2">是否涨停价开盘</th>
            <th rowspan="2">是否在黑名单</th>
            <th rowspan="2">热点板块(其中一个排名前20即可)</th>
            <th colspan="2">封板资金(大于默认封板资金且大于标杆资金)</th>
            <th rowspan="2">30分钟量比(阈值10)</th>
            <th rowspan="2">是否妖股反弹</th>
            <th colspan="5">个股属性(新股不限涨停\高开)</th>
            <th rowspan="2">是否高位板(近三天2次及以上涨停)</th>
            <th rowspan="2">放量阴线10个交易日内</th>
            <th rowspan="2">是否有压力位</th>
            <th rowspan="2">同一板块限制(小于3)</th>
            <th rowspan="2">严格是否买入</th>
            <th rowspan="2">程序过滤原因</th>
        </tr>
        <tr>
            <th>默认封板资金(阈值300万)</th>
            <th>标杆资金(万)</th>
            <th>一年内高开率(阈值0.39,9点50前不限)</th>
            <th>炸板率(阈值0.41)</th>
            <th>涨停数(阈值1)</th>
            <th>最近连板数(阈值2)</th>
            <th>是否新股(60天内上市)</th>
        </tr>
        </thead>
    </table>
    <br/>
    <br/>
</div>
<!--账户收益明细-->


<script type="text/javascript">


function clearCondition(){
    $('#lxzt').val('全部');
    $('#order').val('全部');
    {#var slt=document.getElementById("order");#}
    {#slt.options[0].selected='selected';#}
    $(".filter-option-inner-inner").html("全部");
    $('#is_yzb').val('全部');
    $('#lzt_capital').val('全部');
    $('#gaowei').val('全部');
    $('#big_vol_shade_line').val('全部');
    $('#resistance_section').val('全部');
    $('#is_buy').val('全部');
}

function allIn(flag){
    if (flag){
          $(".stock-checkbox").each(function (key,value) {
        $(value).prop("checked",true);
        var value = $(value).val();
        console.log(value);
        });
    }else{
        $(".stock-checkbox").each(function (key,value) {
        $(value).prop("checked",false);
        var value = $(value).val();
        console.log(value);
        });
    }
}

function getCondition(inputList){

    var chooses=[];

    var lxzt = $('#lxzt').val();
    var each_dict={};
    each_dict['type']='lxzt';
    each_dict['value'] = lxzt;
    if (lxzt!='全部'){
        chooses.push(each_dict);
    }

    var order = $('#order').val();
    var each_dict={};
    each_dict['type']='order';
    each_dict['value'] = order;
    if (order.indexOf('全部')==-1 & order.length>0){
        chooses.push(each_dict);
    }

    var is_yzb = $('#is_yzb').val();
    var each_dict={};
    each_dict['type']='is_yzb';
    each_dict['value'] = is_yzb;
    if (is_yzb!='全部'){
        chooses.push(each_dict);
    }

    var lzt_capital = $('#lzt_capital').val();
    var each_dict={};
    each_dict['type'] = 'lzt_capital';
    each_dict['value'] = lzt_capital;
    if (lzt_capital!='全部'){
        chooses.push(each_dict);
    }

    var gaowei=$('#gaowei').val();
    var each_dict={};
    each_dict['type']='gaowei';
    each_dict['value'] = gaowei;
    if (gaowei!='全部'){
        chooses.push(each_dict);
    }

    var big_vol_shade_line=$('#big_vol_shade_line').val();
    var each_dict={};
    each_dict['type'] = 'big_vol_shade_line';
    each_dict['value'] = big_vol_shade_line;
    if (big_vol_shade_line!='全部'){
        chooses.push(each_dict);
    }

    var resistance_section=$('#resistance_section').val();
    var each_dict={};
    each_dict['type'] = 'resistance_section';
    each_dict['value'] = resistance_section;
    if (resistance_section!='全部'){
        chooses.push(each_dict);
    }

    var is_buy= $('#is_buy').val();
    var each_dict={};
    each_dict['type'] = 'is_buy';
    each_dict['value'] = is_buy;
    if (is_buy!='全部'){
        chooses.push(each_dict);
    }

    var outputList=choosesFilter(inputList,chooses);
    return outputList;
}

function getSelectStock(inputList) {
    var stockList = [];
    $(".stock-checkbox").each(function (key,value) {
        if ($(value).is(':checked')){
            var value = $(value).val();
            stockList.push(value);
        }
    });
    var outputList=inputList.filter(
        function (item) {
            return (stockList.indexOf(item["name"]) > -1);
        });
    return outputList;
}

function choosesFilter(inputList,chooses){
    let tmpList=[];
    if(chooses.length==0){
        tmpList=inputList;
        return tmpList;
    }else{
        for (let choice of chooses){
            tmpList=inputList.filter(
                function (item) {
                    if (jQuery.type(choice.value) == "array"){
                        var flag = false;
                        for (var i=0;i<choice.value.length;i++){
                            if (jQuery.type(item[choice.type])=="string"){
                                if (item[choice.type].indexOf(choice.value[i]) > -1){
                                    flag = true;
                                }
                            }
                        }
                        return flag;
                    }
                    else if (jQuery.type(item[choice.type])=="string"){
                        return item[choice.type].indexOf(choice.value) > -1;
                    }
                }
                );
            inputList=tmpList;
        }
        return tmpList;
    }
}
function reloadData(dataList) {
        var realTable=null;

        realTable = $('#example').DataTable();

        var currentPage = realTable.page();
        realTable.clear();
        realTable.rows.add(dataList);
        realTable.page(currentPage).draw( false );
    }

function loadDate(){

        $.getJSON("/json",function (data) {
            var tradeDayList=data.trade_day;
            for (var i=0;i<tradeDayList.length;i++){
                var date=tradeDayList[i];
                if (date >= "2018-11-01"){
                    if (i==tradeDayList.length-1){
                        var html_str = "<option selected='selected' value='" + date + "'>" + date + "</option>";
                    }else{
                        var html_str = "<option value='" + date + "'>" + date + "</option>";
                    }
                    $('#date').append(html_str);
                }

            }
        })

    }
function refreshConditionFilterData(type){
                var date = $("#date").val();
                $.ajax({
                url: '/conditionFilterData',
                async:true,
                type:'POST',
                data:JSON.stringify({date:date}),
                success:function(result){
                    var obj=JSON.parse(result);
                    var condition_filter_list = obj.condition_filter_list;
                    if (type == "first"){
                        var concept_list = obj.concept_list;
                        var htmlStr = '<option value="全部" selected>全部</option>';
                        for (var i=0;i<concept_list.length;i++){
                            htmlStr += '<option value="'+concept_list[i]+'">'+concept_list[i]+'</option>';
                        }
                        $("#order").html(htmlStr);
                        $("#order").selectpicker("refresh");
                        $("#order").selectpicker("render");
                        var name_list = obj.name_list;
                        var htmlStr = "";
                        for (var i=0;i<name_list.length;i++){
                            htmlStr +=
                            "<label class=\"checkbox-inline\">" +
                            "<input class=\"stock-checkbox\" type=\"checkbox\" id=\"inlineCheckbox1\" value=\"" + name_list[i] +
                            "\">" + name_list[i] +
                            "</label>";
                        }
                        $("#stock_checkbox").html(htmlStr);
                        var zhaban_rate =obj.zhaban_rate;
                        var buy_num = obj.buy_num;
                        var htmlStr = "<p style='font-size:16px;color:blue;'>炸板率:"+String(zhaban_rate)+",robot2买入股票数:"+String(buy_num)+"</p>";
                        $("#zhaban_rate").html(htmlStr);
                        var condition_filter_list_result = condition_filter_list;
                        allIn(true);
                    }
                    if (type == 'condition'){
                        allIn(true);
                        var condition_filter_list_result = getCondition(condition_filter_list);
                    }
                    if(type == 'stock'){
                        clearCondition();
                        var condition_filter_list_result = getSelectStock(condition_filter_list);
                    }
                    reloadData(condition_filter_list_result);
                }
            }
        );

    }

    $("#date").change(function () {
        refreshConditionFilterData("first");
    });

jQuery(document).ready(function () {


        $('#example').DataTable( {
        "paging":   false,
        "ordering": false,
        "info":     false,
        {#"searching":false,#}
        "data": [

  ],

      "columns": [
      { "data": "name" },
      { "data": "datetime" },
      { "data": "concept" },
      { "data": "lxzt" },
      { "data": "order" },
      {"data":"is_yzb"},
      { "data": "is_black" },
      {"data":"is_hot"},
      { "data": "lzt_capital" },
      {"data":"bgzj"},
      { "data": "volume_ratio" },
      {"data":"is_special"},
      { "data": "gk_rate" },
      { "data": "zb_rate" },
      { "data": "zt_num" },
      { "data": "last_lianban" },
      {"data":"is_new_market"},
      {"data":"gaowei"},
      {"data":"big_vol_shade_line"},
      {"data":"resistance_section"},
      {"data":"limit_in_same_block"},
      { "data": "is_buy" },
      {"data":"reason"}
  ]});
        setTimeout(function () {
            refreshConditionFilterData("first");
        },400);
        loadDate();

        });


</script>
<script src="{{url_for('static',filename='js/bootstrap.min.js')}}"></script>
<script src="{{url_for('static',filename='js/bootstrap-select.min.js')}}"></script>
<script src="{{url_for('static',filename='js/defaults-zh_CN.js')}}"></script>
</body>
</html>

